﻿//_______________________________________________________________________________________________________________________
// Copy Right (c) 2010, 2011 SapiensIT Consulting GmbH, all rights reserved
//_______________________________________________________________________________________________________________________
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT
// NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
//_______________________________________________________________________________________________________________________
// PROJECT:     sapiens.at.SharePoint (SharePoint 2010/V4)
//_______________________________________________________________________________________________________________________
// VERSION:     2.0
//_______________________________________________________________________________________________________________________
// DESCRIPTION: 
//_______________________________________________________________________________________________________________________
// LICENSE:     Different licens Types apply to different file types:
//               - Source code files are governed by the MICROSOFT PUBLIC LICENSE (Ms-PL)
//               - Binary files are governed by MSDN CODE GALLERY BINARY LICENSE
//               - Documentation files are governed by CREATIVE COMMONS ATTRIBUTION 3.0 LICENSE
//
//               The licenses can be found at the projects source location.
//               If you do not agree, don't use it!
// 
//_______________________________________________________________________________________________________________________
// Start SharePoint Branding, visit http://www.cliggin.com! Visit us on http://www.sapiens.at! Tell your friends! 
//_______________________________________________________________________________________________________________________
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Sapiens.at.Core.Utils;

namespace Sapiens.at.SharePoint.Extensions
{
    /// <summary>
    /// Extension methods for <see cref="SPContentTypeId"/>, <see cref="SPContentType"/> and <see cref="SPContentTypeCollection"/>.
    /// </summary>
    public static class ContentTypeEx
    {
        /// <summary>
        /// Finds the first contenttype that is parent of cTypeId.
        /// </summary>
        /// <param name="cTypeCollection"></param>
        /// <param name="cTypeId"></param>
        /// <returns></returns>
        public static SPContentType GetContentType(this SPContentTypeCollection cTypeCollection, SPContentTypeId cTypeId)
        {
            foreach (SPContentType cType in cTypeCollection)
            {
                if (cTypeId.IsParentOf(cType.Id)) { return cType; }
            }
            return null;
        }

        /// <summary>
        /// Finds the nearest parent of this contenttype id in a list of ids.
        /// </summary>
        /// <param name="cTypeId"></param>
        /// <param name="ids"></param>
        /// <returns></returns>
        public static SPContentTypeId NearestParent(this SPContentTypeId cTypeId, IEnumerable<SPContentTypeId> ids)
        {
            Validation.ArgumentNotNull(ids, "ids");

            var orderedIds = ids.OrderByDescending(id => id.ToString().Length);
            foreach (var id in orderedIds)
            {
                if (id.IsParentOf(cTypeId)) { return id; }
            }
            throw new KeyNotFoundException();
        }

        ///// <summary>
        ///// Gets the ImageUrl configured in the XMLDocuments of the ContentType.
        ///// <example><XmlDocument NamespaceURI="sapiens.at/sp/Image"><Image Url="/_layouts/Images/SampleIcon.gif" /></XmlDocument></example>
        ///// </summary>
        ///// <param name="cType"></param>
        ///// <returns></returns>
        //public static string getImageUrl(this SPContentType cType)
        //{
        //    const string NAMESPACE = "sapiens.at/sp/Image";
        //    if (!string.IsNullOrEmpty(cType.XmlDocuments[NAMESPACE]))
        //    {
        //        XDocument xDoc = XDocument.Parse(cType.XmlDocuments[NAMESPACE]);
        //        return xDoc.Root.Attribute("Url") != null ? xDoc.Root.Attribute("Url").Value : string.Empty;
        //    }
        //    return string.Empty;
        //}
    }
}
